/*----------------------------------------\ | Capitalize the 1st letter of a sentence or the 1st letter of each | | word in a setence; | |-------------------------------------------| |--------------------------------------------------------------------| |---------------------------| | indata: input data set; | | var: the variable need to be capitalized; | | outdata: if an outdata is given, then the output the dataset to it;| | otherwise saved the dataset to the input dataset; | | dlm: the delimiter to separate word or setence; | | if dlm=' ' or WORD, capitalize the 1st letter of each word in | | the variable; | | if dlm=';', ',' or SENTENCE, capitalize the 1st letter of the | | sentence; | | if dlm=any punctuation, capitalize the 1st letter after it; | |-----------------------------| |--------------------------------------------------------------------| |---------------------------------------| | Example: | | data one; | | input x y cpevent $21.; | | datalines; | | 1.1 1.2 'OneTwo' | | 12 132 tHIS is, AN,EXAMPLE | | 1.1 2.3 1 MONTH FOLLOW-UP | | 2.1 232 4 MONTH FOLLOW-UP | | 1.3 4.1 7 MONTH FOLLOW-UP | | 4.2 100 UNSCHEDULED FOLLOW-UP | | ; | | data two; | | set one; | | %dvupcase(var=keyterm, dlm='/', except=('of', 'at', 'to', 'by'));| | %dvupcase(var=keyterm, dlm=' ', except=('of', 'at', 'to', 'by'));| | %dvupcase(var=keyterm, dlm='(', except=('of', 'at', 'to', 'by'));| | %dvupcase(var=keyterm, dlm=')', except=('of', 'at', 'to', 'by'));| | | run; %print(two); | | Usage: %dvupcase(var=, dlm=' '); | \----------------------------------------*/ %macro dvupcase(var=, dlm=' ', except='')/parmbuff; /*--------------------------------------------\ | Copy Right: Duo Zhou; | | Created: 12-27-2001 12:21am; | | Purpose: Capitalize the 1st letter of a | | sentence or the 1st letter of each | | word in a setence; | \--------------------------------------------*/ %local _dvupi_ _argtmp_ var dlm except; %if (%index(%BQUOTE(&syspbuff), %str(%()) and %index(%BQUOTE(&syspbuff), %str(%())<2) %then %do; %let syspbuff=%substr(%quote(%trim(%left(&syspbuff))), 2, %eval(%length(%quote(%trim(%left(&syspbuff))))-2)); %end; %let syspbuff=%sysfunc(translate(%quote(&syspbuff), %str(%'), %str(%"))); %let syspbuff=%sysfunc(tranwrd(%quote(&syspbuff), ',', ÌÎÍ)); %let _dvupi_=0; %do %while(%length(%nrbquote(%qscan(%nrbquote(&syspbuff), %eval(&_dvupi_+1), %str(,))))); %let _dvupi_=%eval(&_dvupi_+1); %let _argtmp_=%qscan(%nrbquote(&syspbuff), &_dvupi_, %str(,)); %let _argtmp_=%sysfunc(tranwrd(%nrbquote(&_argtmp_), ÌÎÍ, ',')); %if (not %index(%BQUOTE(&_argtmp_), %str(=))) %then %do; %if (%quote(&_dvupi_) eq 1) and (%length(%trim(%quote(%left(%quote(&var))))) le 2) %then %let var=&_argtmp_; %if (%quote(&_dvupi_) eq 2) and (%length(%trim(%quote(%left(%quote(&dlm))))) le 2) %then %let dlm=&_argtmp_; %if (%quote(&_dvupi_) eq 3) and (%length(%trim(%quote(%left(%quote(&except))))) le 2) %then %let except=&_argtmp_; %end; %end; length _last1char_ _last2char_ _dvchar_ $1. _NEWCP_ $200; %if (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(&except))))), %str(%()) eq 1) and (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(%sysfunc(reverse(&except))))))), %str(%))) eq 1) %then %let except=%substr(%quote(%trim(%left(&except))), 2, %eval(%length(%quote(%trim(%left(&except))))-2)); %let except=%sysfunc(translate(%quote(&except), %str(%'), %str(%"))); %let _excpti_=0; %let _excptcnt_=0; &var=trimn(left(compbl(&var))); %do %while(%length(%nrbquote(%qscan(%nrbquote(&except), %eval(&_excpti_+1), %str(,))))); %let _excpti_=%eval(&_excpti_+1); %let _excptmp_=%qscan(%nrbquote(&except), &_excpti_, %str(,)); %if (%quote(%sysfunc(dequote(%quote(&_excptmp_)))) ne) %then %do; %let _excptmp_=%sysfunc(tranwrd(%nrbquote(&_excptmp_), ÌÎÍ, ',')); %let _excptfr&_excpti_=%sysfunc(dequote(&_excptmp_)); %let _excptto&_excpti_=®%trim(%left(&_excpti_)); &var=tranwrd(&var, "&&_excptfr&_excpti_", "&&_excptto&_excpti_"); %end; %else %let _excptfr&_excpti_=; %end; %let _excptcnt_=&_excpti_; %if (%quote(&dlm) eq ) %then %let dlm=' '; _last1char_=' '; _last2char_=' '; _NEWCP_=' '; %if (%length(&dlm)>1) %then %do; do _dvari_=1 to length(&var); _dvchar_=substr(&var,_dvari_,1); if ((_last1char_=&dlm) or (_last1char_=' ' and _last2char_=&dlm) or (_dvari_=1)) then do; _dvchar_=upcase(_dvchar_); end; if (_last1char_=' ' or _last1char_=&dlm or (_last1char_=' ' and _last2char_=&dlm) or _dvari_=1) then do; if not rxmatch(rxparse("'/'|'\'|'_'|'-'|'+'|'='|'|'|'<'|'>'|'*'|'~'|'`'|'@'|'#'|'$'|'('|'^'"), _last1char_) and _last1char_ ne "'" then do; _NEWCP_=compbl(_NEWCP_)||_dvchar_; end; else do; _NEWCP_=trimn(left(_NEWCP_))||_dvchar_; end; end; else do; _NEWCP_=trimn(left(_NEWCP_))||_dvchar_; end; _last2char_=_last1char_; _last1char_=_dvchar_; end; %end; %else %do; _NEWCP_=upcase(substr(&var,1,1))||substr(&var,2,(length(&var)-1)); %end; %if (%quote(&_excptcnt_) ge 1) %then %do; %do _excpti_=1 %to &_excptcnt_; %if (%quote(&&_excptfr&_excpti_) ne) %then %do; _NEWCP_=tranwrd(_NEWCP_, "&&_excptto&_excpti_", "&&_excptfr&_excpti_"); %end; %end; %end; &var=_NEWCP_; drop _NEWCP_ _dvari_ _dvchar_ _last1char_ _last2char_; %mend dvupcase;